<template>
  <div>
    <h1>添加视频</h1>
    <van-field label="章节名称" v-model="form.name"></van-field>
    分类：<select name="" id="" v-model="form.course_id">

      <option :value="i.id" v-for="i in courses">{{ i.title }}</option>
    </select>
    <br/>
    视频：<van-uploader :after-read="add_mp4" accept=".mp4"></van-uploader>
    <br/>
    <van-button round @click="add">添加</van-button>
  </div>
</template>

<script>
export default {
  name: "add_jie",
  data() {
    return {
      form: {'video_url': ''},
      courses: []
    }
  },
  methods: {
    getcourses() {
      this.$axios.get('/courses/courses').then(
        res => {
          if (res.data.code == 200) {
            this.courses = res.data.list
          }
        }
      )
    },
    add() {
      this.$axios.post('/courses/jie', this.form).then(res => {
        if (res.data.code == 200) {
          alert('添加成功')
          this.form = {}
        }
      })

    },
    add_mp4(file) {
      if (this.form.course_id) {
        let formdata = new FormData()
        formdata.append('course_id', this.form.course_id)
        formdata.append('file', file.file)
        this.$axios.post('/courses/addmp4', formdata)
          .then(res => {
            if (res.data.code == 200) {
              alert('ok')
              this.form.video_url = res.data.url
            }
          })

      } else {
        alert('请先选课程')
      }

    }
  },
  mounted() {
    this.getcourses()
  }
}
</script>

<style scoped>

</style>
